<?php
include "soar/soar.inc";

function INPUT($paras)
{/*{{{*/
    if(!isset($paras['from']) || !isset($paras['talk']))
    {
        return "PARA_INVALID";
    }

    // 连接server
    $request='';
    para::put_item($request, 'INPUT', $paras['from'], $paras['talk']);
    $timeout=5;
    $response=sock::callonce('localhost:59991', $request, $timeout);
    if($response === false)
    {
        errlog::add("%s|%s: call chat server fail",
                basename(__FILE__), __METHOD__);
        return 'COMM_FAIL';
    }
    errlog::add("%s|%s: response:%s",
                basename(__FILE__), __METHOD__, $response);
    $curr=0;
    $errcode=para::get_item($response, $curr);
    if(is_null($errcode))
    {
        errlog::add("%s|%s: errcode invalid",
                basename(__FILE__), __METHOD__);
        return 'ERRCODE_INVALID';
    }
    if($errcode != 'OK')
    {
        errlog::add("%s|%s: errcode:%s",
                basename(__FILE__), __METHOD__, $errcode);
        return $errcode;
    }

    return 'OK';
}/*}}}*/

function METHODS($method, $paras)
{/*{{{*/
    switch($method)
    {
        case 'INPUT':
            $response=INPUT($paras[0]);
            break;
        default:
            $response="SVC_NOT_FOUND";
            break;
    }
    return $response; 
}/*}}}*/

function main()
{/*{{{*/
    $server=xmlrpc_server_create();
    xmlrpc_server_register_method($server, 'INPUT', 'METHODS');
    $request=$GLOBALS['HTTP_RAW_POST_DATA'];
    $response=xmlrpc_server_call_method($server, $request, null,
            array('escaping'=>array('markup'),'encoding'=>'utf-8'));
    header("Content-Type:text/xml");
    print $response;
    xmlrpc_server_destroy($server);
}/*}}}*/

soar::init();
main();
?>
